#ifndef FILESOCKET_H
#define FILESOCKET_H

#include <QObject>
#include <QTcpSocket>
#include <QFile>
#include <QApplication>

class FileSocket : public QObject
{
    Q_OBJECT
public:
    explicit FileSocket(QObject *parent = 0, QTcpSocket *tcpSocket = NULL);
    ~FileSocket();

    void Close();
    bool CheckUserId(const qint32 nId, const qint32 &winId);

    // 文件传输完成
    void FileTransFinished();

    void StartTransferFile(QString fileName);
signals:
    void signalConnected();
    void signalDisConnected();

    void signalRecvFinished(int id, const QJsonValue &json);

private:
    /************* Receive file *******************/
    quint64 loadSize;
    quint64 bytesReceived;  //已收到数据的大小
    quint64 fileNameSize;  //文件名的大小信息
    QString fileReadName;   //存放文件名
    QByteArray inBlock;   //数据缓冲区
    quint64 ullRecvTotalBytes;  //数据总大小
    QFile *fileToRecv;  //要发送的文件

    QTcpSocket *m_tcpSocket;

    /************* Receive file *******************/
    quint16 blockSize;  //存放接收到的信息大小
    QFile *fileToSend;  //要发送的文件
    quint64 ullSendTotalBytes;  //数据总大小
    quint64 bytesWritten;  //已经发送数据大小
    quint64 bytesToWrite;   //剩余数据大小
    QByteArray outBlock;  //数据缓冲区，即存放每次要发送的数据


    bool m_bBusy;

    // 需要转发的用户id
    qint32 m_nUserId;
    // 当前用户的窗口好友的id
    qint32 m_nWindowId;
private:
    void InitSocket();

public slots:

private slots:
    void displayError(QAbstractSocket::SocketError); // 显示错误
    // 文件接收
    void SltReadyRead();
    // 发送
    void SltUpdateClientProgress(qint64 numBytes);
};

#endif // FILESOCKET_H
